﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Client.p2p
{
    public class MetaDataBuilder
    {
        static public byte[] TimeAndFile(DateTime time, string filename)
        {
            Stream stream = new MemoryStream();
            string strTime = time.ToString();
            byte[] buffer = BitConverter.GetBytes(strTime.Length);
            stream.Write(buffer, 0, buffer.Length);
            buffer = Encoding.ASCII.GetBytes(strTime);
            stream.Write(buffer, 0, buffer.Length);
            buffer = File.ReadAllBytes(filename);
            stream.Write(BitConverter.GetBytes(buffer.Length), 0, 4);
            stream.Write(buffer, 0, buffer.Length);
            buffer = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(buffer, 0, buffer.Length);
            return buffer;
        }

        static public byte[] TimeAndContent(DateTime time, string content, Encoding encoding)
        {
            Stream stream = new MemoryStream();
            string strTime = time.ToString();
            byte[] buffer = BitConverter.GetBytes(strTime.Length);
            stream.Write(buffer, 0, buffer.Length);
            buffer = Encoding.ASCII.GetBytes(strTime);
            stream.Write(buffer, 0, buffer.Length);
            buffer = BitConverter.GetBytes(encoding.GetByteCount(content));
            stream.Write(buffer, 0, buffer.Length);
            buffer = encoding.GetBytes(content);
            stream.Write(buffer, 0, buffer.Length);
            buffer = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(buffer, 0, buffer.Length);
            return buffer;
        }

        static public byte[] FileAttribute(FileAttr fileAttr)
        {
            Stream stream = new MemoryStream();
            byte[] buffer = BitConverter.GetBytes(fileAttr.No);
            stream.Write(buffer, 0, buffer.Length);
            buffer = BitConverter.GetBytes(fileAttr.FileSize);
            stream.Write(buffer, 0, buffer.Length);
            buffer = BitConverter.GetBytes(fileAttr.FileName.Length);
            stream.Write(buffer, 0, buffer.Length);
            buffer = Encoding.Unicode.GetBytes(fileAttr.FileName);
            stream.Write(buffer, 0, buffer.Length);
            buffer = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(buffer, 0, buffer.Length);
            return buffer;
        }

        static public byte[] FileTransferNoAndPort(int no, int port)
        {
            byte[] buffer = new byte[8];
            Array.Copy(BitConverter.GetBytes(no), 0, buffer, 0, 4);
            Array.Copy(BitConverter.GetBytes(port), 0, buffer, 4, 4);
            return buffer;
        }

        static public byte[] FileTransferNoAndSource(int no, string source)
        {
            Stream stream = new MemoryStream();
            byte[] buffer = BitConverter.GetBytes(no);
            stream.Write(buffer, 0, buffer.Length);
            buffer = BitConverter.GetBytes(source.Length);
            stream.Write(buffer, 0, buffer.Length);
            buffer = Encoding.Unicode.GetBytes(source);
            stream.Write(buffer, 0, buffer.Length);
            buffer = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(buffer, 0, buffer.Length);
            return buffer;
        }

/*
        static public byte[] PortAndTime(int port, DateTime time)
        {
            Stream stream = new MemoryStream();
            string strTime = time.ToString();
            byte[] buffer = BitConverter.GetBytes(strTime.Length);
            stream.Write(buffer, 0, buffer.Length);
            buffer = Encoding.ASCII.GetBytes(strTime);
            stream.Write(buffer, 0, buffer.Length);
            buffer = File.ReadAllBytes(filename);
            stream.Write(buffer, 0, buffer.Length);
            buffer = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(buffer, 0, buffer.Length);
            return buffer;
        }
*/
    }

    public class MetaDataParser
    {
        static public FileAttr FileAttribute(byte[] bytes)
        {
            FileAttr fileAttr = new FileAttr();
            fileAttr.No = BitConverter.ToInt32(bytes, 0);
            fileAttr.FileSize = BitConverter.ToInt64(bytes, 4);
            int size = BitConverter.ToInt32(bytes, 12);
            fileAttr.FileName = Encoding.Unicode.GetString(bytes, 16, size * 2);
            return fileAttr;
        }

        static public int[] FileTransferNoAndPort(byte[] bytes)
        {
            return new int[] { BitConverter.ToInt32(bytes, 0), BitConverter.ToInt32(bytes, 4) };
        }

        static public object[] FileTransferNoAndSource(byte[] bytes)
        {
            return new object[] { BitConverter.ToInt32(bytes, 0),
                Encoding.Unicode.GetString(bytes, 8, BitConverter.ToInt32(bytes, 4) * 2) };
        }
    }
}
